<?php



// +----------------------------------------------------------------------



// | ThinkPHP [ WE CAN DO IT JUST THINK ]



// +----------------------------------------------------------------------



// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.



// +----------------------------------------------------------------------



// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )



// +----------------------------------------------------------------------



// | Author: 流年 <liu21st@gmail.com>



// +----------------------------------------------------------------------



use think\Db;



// 应用公共文件



function getIP() {



    if (@$_SERVER["HTTP_X_FORWARDED_FOR"])



        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];



    else if (@$_SERVER["HTTP_CLIENT_IP"])



        $ip = $_SERVER["HTTP_CLIENT_IP"];



    else if (@$_SERVER["REMOTE_ADDR"])



        $ip = $_SERVER["REMOTE_ADDR"];



    else if (@getenv("HTTP_X_FORWARDED_FOR"))



        $ip = getenv("HTTP_X_FORWARDED_FOR");



    else if (@getenv("HTTP_CLIENT_IP"))



        $ip = getenv("HTTP_CLIENT_IP");



    else if (@getenv("REMOTE_ADDR"))



        $ip = getenv("REMOTE_ADDR");



    else



        $ip = "未知";



    return $ip;



}



/**特定关闭窗口并刷新父类查窗口*/



function backUrl(){



    return "<script>window.parent.location.reload();var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index);</script>";



}



/*删除文章内容图片（也就是删除编辑器上传的图片）*/



function delete_img($content,$order='ALL'){



    $pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";  



    preg_match_all($pattern,$content,$match);  



    if(isset($match[1])&&!empty($match[1])){  



       if($order==='ALL'){  



           $host = 'http://' . $_SERVER['HTTP_HOST'] . '/';



           for ($i=0; $i < count($match[1]); $i++) {   

              $filepath = str_replace(config('view_replace_str.__ROOT__'.'/upload/attached/'),IMG_UPLOADS,$match[1][$i]);



              if(is_file($filepath))



                  @unlink($filepath);



           }



           //return $match[1];



         }  



       if(is_numeric($order)&&isset($match[1][$order])){  



            return $match[1][$order];  



        }  



    }    



}



//获取分类的名称



function typename($id,$field='id',$dbname='mo_city'){
    $data = Db::table($dbname)->where($field,"=",$id)->find();
    return $data['name'];
}



function typename_tidai($id){







    $data = Db::table('mo_type_s')->where('id',$id)->find();







    return $data['tidai'];







}



//根据城市ID获取城市名称



function cityname($id){







    $data = Db::table('mo_city')->where('id',$id)->find();







    return $data['name'];







}



//根据会员的ID获取会员的字段



function userField($uid,$field){







  $data = Db::table('mo_user a')



          ->join('mo_user_basic b','a.id=b.mid','left')



          ->where('a.id',$uid)



          ->field($field)



          ->find();







  return $data[$field];







}



//返回周几函数 



function backWeek($n){



  switch($n){



    case 1 : $week = "周一"; break;



    case 2 : $week = "周二"; break;



    case 3 : $week = "周三"; break;



    case 4 : $week = "周四"; break;



    case 5 : $week = "周五"; break;



    case 6 : $week = "周六"; break;



    default: $week = "周日"; break;



  }



  return $week;



}



//获取当前会员在当天是否已经预定过啦



function setYunyueO($yid,$mid,$field,$str){



  //先判断会员是否是收费会员，如果是用1语句，不是用2语句



  $shoufei = Db::table("mo_user_vip")->where("mid",$mid)->field('shoufei')->find();



  if($shoufei['shoufei']==1){



    $ycid = Db::table("mo_yuyue_o a")



            ->join("mo_yuyue_c b","a.ycid=b.id")



            ->where("b.kid",$str)



            ->where("a.yid",$yid)



            ->where('a.zhifu',1)



            ->limit(0,1)



            ->Field("a.".$field)



            ->find();



  }else{



     $ycid = Db::table("mo_yuyue_o a")



            ->join("mo_yuyue_c b","a.ycid=b.id")            



            ->where("b.kid",$str)



            ->where("a.yid",$yid)



            ->limit(0,1)



            ->Field("a.".$field)



            ->find();



  }







  return $ycid[$field];







}

function getLever($uid){



  $lever = Db::table("mo_guanli")->where("id",$uid)->value("ma_l");



  return $lever;



}





function getJson($url){



  $ch = curl_init();



  curl_setopt($ch, CURLOPT_URL, $url);



  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 



  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 



  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);



  $output = curl_exec($ch);



  curl_close($ch);



  return json_decode($output, true);



}


function getdistance($lng1, $lat1, $lng2, $lat2) {
    // 将角度转为狐度
    $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);
    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
    return $s;
} 


//获取第一张图片作为封面
function getImgpath($Arr,$type=0){
  $imgpathArr = json_decode($Arr,true);
  if($type==1)
    return $imgpathArr;
  else
    return $imgpathArr[0]['imgpath']?$imgpathArr[0]['imgpath']:0;
}


/*
* 用于手机端登录模块跳转
*/
function ismobile() {
// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
return true;
//此条摘自TPM智能切换模板引擎，适合TPM开发
if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
return true;
//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset ($_SERVER['HTTP_VIA']))
//找不到为flase,否则为true
return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
//判断手机发送的客户端标志,兼容性有待提高
if (isset ($_SERVER['HTTP_USER_AGENT'])) {
$clientkeywords = array(
'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
);
//从HTTP_USER_AGENT中查找手机浏览器的关键字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
return true;
}
}
//协议法，因为有可能不准确，放到最后判断
if (isset ($_SERVER['HTTP_ACCEPT'])) {
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
return true;
}
}
return false;
}

